package com.wyp168.renrenfilm.core.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wyp168.renrenfilm.core.entity.FilmLabelRelationEntity;
import com.wyp168.renrenfilm.core.feign.RenrenFastFeign;
import com.wyp168.renrenfilm.core.service.FilmLabelRelationService;
import com.wyp168.renrenfilm.core.service.RequireService;
import com.wyp168.renrenfilm.core.vo.LabelRelationVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.wyp168.renrenfilm.core.entity.FilmLabelEntity;
import com.wyp168.renrenfilm.core.service.FilmLabelService;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wyp168
 * @email 2721255249@qq.com
 * @date 2022-11-16 18:36:43
 */
@RestController
@RequestMapping("film/filmlabel")
public class FilmLabelController {
    @Autowired
    private FilmLabelService filmLabelService;

    @Autowired
    private RequireService requireService;

    @Autowired
    private FilmLabelRelationService filmLabelRelationService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageInfo<FilmLabelEntity> filmLabelEntityPageInfo = filmLabelService.queryPage(params);
        return R.ok().put("page", filmLabelEntityPageInfo);
    }


    /**
     * 获取所有标签信息
     */
    @GetMapping("/lists")
    public R lists(){
        List<FilmLabelEntity> list = filmLabelService.list();
        return R.ok().put("data", list);
    }

    /**
     * 给电影绑定标签
     */
    @PostMapping("/bindingLabel")
    public R bindingLabel(HttpServletRequest request, @RequestBody LabelRelationVO labelRelationVO) {
        if (Objects.isNull(labelRelationVO.getFilmId()) || CollectionUtils.isEmpty(labelRelationVO.getLabelIds())) {
            return R.error("参数错误");
        }

        String username = requireService.getLoginUserByRequest(request);

        filmLabelRelationService.bindingLabel(username, labelRelationVO.getFilmId(), labelRelationVO.getLabelIds());

        return R.ok();
    }


    /**
     * 根据filmId获取绑定的标签信息
     */
    @GetMapping({"/getLabels/{filmId}", "/getLabels"})
    public R getLabels(@PathVariable("filmId") Long filmId) {
        if (Objects.isNull(filmId)) {
            return R.error("id不能为空");
        }
        QueryWrapper<FilmLabelRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("film_id", filmId);
        List<FilmLabelRelationEntity> filmLabelRelationEntities = filmLabelRelationService.list(wrapper);
        if (CollectionUtils.isEmpty(filmLabelRelationEntities)) {
            return R.ok().put("data", new ArrayList<>());
        }
        List<Long> labelIds = filmLabelRelationEntities.stream().map(FilmLabelRelationEntity::getLabelId).collect(Collectors.toList());
        List<FilmLabelEntity> filmLabelEntities = filmLabelService.listByIds(labelIds);
        return R.ok().put("data", filmLabelEntities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		FilmLabelEntity filmLabel = filmLabelService.getById(id);

        return R.ok().put("filmLabel", filmLabel);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(HttpServletRequest request, @RequestBody FilmLabelEntity filmLabel){
        String username = requireService.getLoginUser(request.getHeader("token"));
        filmLabel.setCreatedBy(username);
        filmLabel.setModifiedBy(username);

        filmLabelService.save(filmLabel);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(HttpServletRequest request, @RequestBody FilmLabelEntity filmLabel){
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return R.error("token为空");
        }
        String username = requireService.getLoginUser(token);
        filmLabel.setModifiedBy(username);
        filmLabelService.updateById(filmLabel);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		filmLabelService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
